<?php

/**
 * This is the model class for table "{{category}}".
 *
 * The followings are the available columns in table '{{category}}':
 * @property string $category_id
 * @property string $parentid
 * @property string $user_id
 * @property string $arrparentid
 * @property integer $child
 * @property string $arrchildid
 * @property string $name
 * @property string $slug
 * @property integer $count
 * @property integer $displayorder
 * @property integer $created
 * @property integer $modified
 */
class Category extends CActiveRecord {
    /**
     * Returns the static model of the specified AR class.
     * @return Category the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{category}}';
    }

    /**
     * add default created modified time
     */

    public function behaviors() {
        return array(
                'AutoTimestampBehavior' => array(
                        'class' => 'application.components.AutoTimestampBehavior',
                //You can optionally set the field name options here
                )
        );
    }
    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
                array(' name', 'required'),
                //array('child, count, displayorder, created, modified', 'numerical', 'integerOnly'=>true),
                array('parentid, user_id', 'length', 'max'=>20),
                array('name, slug', 'length', 'max'=>255),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('category_id, parentid, user_id, arrparentid, child, arrchildid, name, slug, count, displayorder, created, modified', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
                'post' => array(self::MANY_MANY,'Posts','hi_cate_post(cate_id,post_id)'),
        );
    }

    /**
     * add default desc
     */

    public function scopes() {
        return array(
                'published'=>array(
                        'order'=>'created desc',
                ),
        );

    }
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
                'category_id' => 'Category',
                'parentid' => 'Parentid',
                'user_id' => 'User',
                'arrparentid' => 'Arrparentid',
                'child' => 'Child',
                'arrchildid' => 'Arrchildid',
                'name' => 'Name',
                'slug' => 'Slug',
                'count' => 'Count',
                'displayorder' => 'Displayorder',
                'created' => 'Created',
                'modified' => 'Modified',
        );
    }

    public function getLink() {
        return CHtml::link($this->name,Yii::app()->createUrl("category/index",array('id'=>$this->category_id,'title'=>$this->name)));
    }

    public static function string2array($cateid) {
        return preg_split('/\s*,\s*/',trim($cateid),-1,PREG_SPLIT_NO_EMPTY);
    }

    public function CateCount($cateid) {
        foreach (self::string2array($cateid) as $cid) {
            $model=Category::model()->findByPk($cid);
            $model->count=CatePost::model()->count("cate_id=".$cid);
            //$model->update();
        }
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('category_id',$this->category_id,true);
        $criteria->compare('parentid',$this->parentid,true);
        $criteria->compare('user_id',$this->user_id,true);
        $criteria->compare('arrparentid',$this->arrparentid,true);
        $criteria->compare('child',$this->child);
        $criteria->compare('arrchildid',$this->arrchildid,true);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('slug',$this->slug,true);
        $criteria->compare('count',$this->count);
        $criteria->compare('displayorder',$this->displayorder);
        $criteria->compare('created',$this->created);
        $criteria->compare('modified',$this->modified);

        return new CActiveDataProvider(get_class($this), array(
                        'criteria'=>$criteria,
        ));
    }
}